package me.wangqu.weiyouqu.web.rest.business.dao.impl;

import me.wangqu.weiyouqu.web.rest.business.dao.RanklistDao;
import me.wangqu.weiyouqu.web.rest.business.model.RankModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by joln on 2016/6/27.
 */
@Repository
public class RanklistDaoImpl implements RanklistDao {
    private final Logger log = LoggerFactory.getLogger(RanklistDaoImpl.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<RankModel> getAllRanklists() {
        log.debug("获取排行榜列表");
        StringBuffer sql = new StringBuffer(128);
        sql.append("SELECT u.phone as login,u.income as income,u.id as uid FROM userdata u  ORDER BY income DESC limit 20");
        return jdbcTemplate.query(sql.toString(), new RowMapper<RankModel>() {
            @Override
            public RankModel mapRow(ResultSet rt, int i) throws SQLException {
                RankModel rankModel = new RankModel();
                rankModel.setLogin(rt.getString("login"));
                rankModel.setUid(rt.getInt("uid"));
                rankModel.setIncome(rt.getBigDecimal("income"));
                return rankModel;
            }
        });


    }
}
